package com.cipe.cmrs.converter;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;

import com.cipe.cmrs.bean.PaymentRequestBean;
import com.cipe.cmrs.bean.PaymentRequestLineItemBean;
import com.cipe.cmrs.model.Payee;
import com.cipe.cmrs.model.Paymentrequest;
import com.cipe.cmrs.model.Paymentrequestlineitem;
import com.cipe.cmrs.model.Paymenttype;

public class PaymentRequestToPaymentRequestBean implements Converter<Paymentrequest, PaymentRequestBean> {

    private ConverterRegistry converterRegistry;
    private ConversionService conversionService;

    /**
     * @param converterRegistry
     *            the converterRegistry to set
     */
    public void setConverterRegistry(ConverterRegistry converterRegistry) {
        this.converterRegistry = converterRegistry;
    }

    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    public void init() {
        converterRegistry.addConverter(this);
    }

    public PaymentRequestBean convert(Paymentrequest source) {
        PaymentRequestBean bean = new PaymentRequestBean();
        if (source != null) {
            if (source.getCreatedBy() != null) {
                bean.setCreatedById(source.getCreatedBy().getId());
                bean.setCreatedBy(source.getCreatedBy().getFullName());
            }
            bean.setCreationDate(source.getCreationDate());
            if (source.getUpdatedBy() != null) {
                bean.setUpdatedById(source.getUpdatedBy().getId());
                bean.setUpdatedBy(source.getUpdatedBy().getFullName());
            }
            bean.setUpdationDate(source.getUpdationDate());
            bean.setId(source.getId());
            Paymenttype type = source.getPaymenttype();
            if (type != null) {
                bean.setPaymentTypeId(type.getId());
                bean.setPaymentTypeDesc(type.getPaymenttypename());
            }
            bean.setPaymentIdentifiedInformation(source.getPaymentIdentifiedInformation());
            bean.setReceivedFromAccountingDate(source.getReceivedFromAccountingDate());
            bean.setPaymentMethod(source.getPaymentmethod());
            Payee payee = source.getPayee();
            if (payee != null) {
                bean.setPayeeId(payee.getId());
                bean.setPayeeDesc(payee.getPayeeName());
                bean.setPrevPayeeId(payee.getId());
            }
            bean.setPurposeOfExpenditure(source.getPurposeOfExpenditure());
            bean.setSpecialInstructions(source.getSpecialInstructions());
            bean.setInvoiceRequestDate(source.getInvoiceRequestDate());
            bean.setPrevInvoiceRequestDate(source.getInvoiceRequestDate());
            if (StringUtils.isNotBlank(source.getInvoicenumber())) {
                bean.setInvoicenumber(source.getInvoicenumber());
                bean.setPrevInvoicenumber(source.getInvoicenumber());
            }
            bean.setDateToFinanace(source.getDateToFinanace());
            bean.setDateResubmitted(source.getDateresubmitted());
            bean.setStatusCode(source.getStatusCode());
            bean.setDateCancelled(source.getDatecancelled());
            bean.setDateCancelRequested(source.getDateCancelRequested());         
            bean.setAmount(source.getAmount() != null ? source.getAmount() : BigDecimal.ZERO);
            bean.setPrevAmount(source.getAmount() != null ? source.getAmount() : BigDecimal.ZERO);
            bean.setCurrentlyresponsible(source.getCurrentlyresponsible());
            bean.setDateforwardedforsignature(source.getDateforwardedforsignature());
            bean.setDatesubmittedforpayment(source.getDatesubmittedforpayment());
            bean.setDatePaid(source.getDatepaid());
            bean.setDateRejected(source.getDaterejected());
            bean.setComments(source.getComments());
            bean.setReasonrejected(source.getReasonrejected());
            bean.setPaymentConfirmationNumber(source.getPaymentConfirmationNumber());
            bean.setSourceDocumentType(source.getSourceDocumentType());
            bean.setSixFiveSeventyVerify(source.get_6570Verify());
            if (source.getPaymentrequestlineitems() != null && source.getPaymentrequestlineitems().size() > 0) {
                for (Paymentrequestlineitem lineItem : source.getPaymentrequestlineitems()) {
                    bean.getLineItemBeans().add(conversionService.convert(lineItem, PaymentRequestLineItemBean.class));
                }
            }

            Collections.sort(bean.getLineItemBeans(), new Comparator<PaymentRequestLineItemBean>() {
                public int compare(PaymentRequestLineItemBean o1, PaymentRequestLineItemBean o2) {
                    if (o1.getLineItemNumber() != null && o2.getLineItemNumber() != null) {
                        return o1.getLineItemNumber().compareTo(o2.getLineItemNumber());
                    } else if (o1.getLineItemNumber() != null) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            });
        }
        return bean;
    }
}
